package com.shadow.study.java.base.util.captcha;

import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

@Slf4j
public class TestCaptcha {

    public static void main(String[] args) {
        test2();
        test1();
    }

    private static void test2() {
        String code = CaptchaUtils.generatorCode();
        log.info(code);
    }

    private static void test1() {
        String code = CaptchaUtils.generatorCode();
        log.info(code);
        BufferedImage bufferedImage = CaptchaUtils.generatorVCodeImage(code, false);
        log.info(bufferedImage.toString());
        try {
            //产生新的gif文件，文件名test.png
            ImageIO.write(bufferedImage, "gif",new File("D:\\workspace\\generator\\file\\test.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void generateLoginCpacha(HttpServletRequest request, HttpServletResponse reponse) throws IOException{
        //通过对象的generatorVCode()方法，生成验证码
        String generatorVCode = CaptchaUtils.generatorCode();
        request.getSession().setAttribute("loginCapcha", generatorVCode);
        //通过对象创建验证码图片
        BufferedImage generatorRotateVCodeImage = CaptchaUtils.generatorRotateVCodeImage(generatorVCode, true);
        ImageIO.write(generatorRotateVCodeImage, "gif", reponse.getOutputStream());
    }
}
